package tt.dz.biz.service.smsConfigure;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.smsConfigure.SmsConfigureDAOImpl;
import tt.dz.common.BusinessException;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.vo.SmsConfigureVO;
import tt.dz.vo.SmsParameterShowVO;
import tt.dz.vo.SmsParameterVO;
import tt.dz.vo.SmsSendRecord;

@Service("smsConfigureService")
public class SmsConfigureService
{
  @Resource(name="smsConfigureDAO")
  private SmsConfigureDAOImpl smsConfigureDAO;
  
  public int saveSmsConfigure(SmsConfigureVO smsConfigureVO, List<SmsParameterShowVO> smsParameterVOList)
  {
    String id = null;
    int result = -1;
    if (null == smsConfigureVO.getId())
    {
      smsConfigureVO.setId(UUID.randomUUID().toString());
      id = smsConfigureVO.getId();
      try
      {
        result = this.smsConfigureDAO.insertSmsConfigure(smsConfigureVO);
      }
      catch (Exception e)
      {
        DzErrorCode.ExceptionErrorCode ec = DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION;
        throw new BusinessException(ec.value(), ec.getDesc(), "插入短息平台配置相关数据异常");
      }
    }
    else
    {
      id = smsConfigureVO.getId();
      try
      {
        result = this.smsConfigureDAO.updateSmsConfigure(smsConfigureVO);
      }
      catch (Exception e)
      {
        DzErrorCode.ExceptionErrorCode ec = DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION;
        throw new BusinessException(ec.value(), ec.getDesc(), "更新短息平台配置相关数据异常");
      }
    }
    if (null != id) {
      try
      {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("id", id);
        result = deleteSmsParameter(paramMap);
      }
      catch (Exception e)
      {
        DzErrorCode.ExceptionErrorCode ec = DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION;
        throw new BusinessException(ec.value(), ec.getDesc(), "删除短息配置参数异常");
      }
    }
    if ((null != smsParameterVOList) && (!smsParameterVOList.isEmpty())) {
      try
      {
        for (SmsParameterShowVO smsParameterShowVO : smsParameterVOList)
        {
          SmsParameterVO smsParameterVO = new SmsParameterVO();
          smsParameterVO.setSmsConfigId(id);
          smsParameterVO.setParamName(smsParameterShowVO.getParamName());
          smsParameterVO.setParamKey(smsParameterShowVO.getParamKey());
          smsParameterVO.setParamValue(smsParameterShowVO.getParamValue());
          result = saveSmsParameter(smsParameterVO);
        }
      }
      catch (Exception e)
      {
        DzErrorCode.ExceptionErrorCode ec = DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION;
        throw new BusinessException(ec.value(), ec.getDesc(), "保存短信设置参数异常");
      }
    }
    return result;
  }
  
  public int saveSmsParameter(SmsParameterVO smsParameterVO)
  {
    if (smsParameterVO != null) {
      return this.smsConfigureDAO.insertSmsParameter(smsParameterVO);
    }
    return -1;
  }
  
  public List<SmsConfigureVO> getSmsConfigureVOlist()
  {
    return this.smsConfigureDAO.getSmsConfigureVOlist();
  }
  
  public List<SmsParameterShowVO> getSmsParameterVOList(Map<String, Object> paramMap)
  {
    return this.smsConfigureDAO.getSmsParameterVOList(paramMap);
  }
  
  public int deleteSmsConfigure(Map<String, Object> paramMap)
  {
    return this.smsConfigureDAO.deleteSmsConfigure(paramMap);
  }
  
  public int deleteSmsParameter(Map<String, Object> paramMap)
  {
    return this.smsConfigureDAO.deleteSmsParameter(paramMap);
  }
  
  public List<SmsConfigureVO> getConfigsByMerchantCode(String merchantCode)
  {
    return this.smsConfigureDAO.getConfigsByMerchantCode(merchantCode);
  }
  
  public Map<String, String> convertParamMap(SmsConfigureVO smsConfig)
  {
    return this.smsConfigureDAO.convertParamMap(smsConfig);
  }
  
  public void saveSmsSendRecord(SmsSendRecord smsSendRecord)
  {
    this.smsConfigureDAO.saveSmsSendRecord(smsSendRecord);
  }
}
